package com.softeem.ossmusic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softeem.ossmusic.comm.R;
import com.softeem.ossmusic.entity.User;
import com.softeem.ossmusic.service.UserService;
import com.softeem.ossmusic.mapper.UserMapper;
import org.springframework.stereotype.Service;

/**
* @author softeem
* @description 针对表【user】的数据库操作Service实现
* @createDate 2025-10-28 11:42:45
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Override
    public R login(String username, String password) {
        //根据用户名查询用户  where username=?
        QueryWrapper<User> query = Wrappers.query(User.class);
        query.eq("username", username);
        //执行查询
        User user = getOne( query);
        if(user == null){
            return R.error().setMsg("用户不存在");
        }
        //账号是否禁用
        if(user.getStatus() == 0){
            return R.error().setMsg("账号被禁用");
        }
        //检查密码是否错误
        if(!user.getPassword().equals(password)){
            return R.error().setMsg("密码错误");
        }
        //登录成功
        return R.success().setData(user);
    }

    @Override
    public R register(User user) {
        //查询是否存在该用户
        QueryWrapper<User> query = Wrappers.query(User.class);
        query.eq("username", user.getUsername());
        if(exists(query)){
            return R.error().setMsg("用户已存在");
        }
        if(save(user)){
            return R.success().setData(user);
        }
        return R.error().setMsg("注册失败");
    }
}




